//
//  HTVideoTableViewCell.m
//  HeartTravel
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTVideoTableViewCell.h"
#import "UIImageView+WebCache.h"

#define kWidth (SCREEN_WIDTH - 40 - 165)/4
@implementation HTVideoTableViewCell

- (void)setConfigureModel:(HTVideoMainListModel *)configureModel{
    _configureModel = configureModel;
    [self.illView.iconImageView sd_setImageWithURL:[NSURL URLWithString:configureModel.u.headerArray[0]]];
    self.illView.nameLabel.text = configureModel.u.name;
    self.illView.timeLabel.text = configureModel.passtime;
    // 标题label
    _titleLabel.text = configureModel.text;
    _titleLabel.frame = CGRectMake(10, 60, SCREEN_WIDTH - 30, [HTVideoTableViewCell heightOfTitle:configureModel]);
    CGFloat hgt = [configureModel.video.height floatValue];
    CGFloat wid = [configureModel.video.width floatValue];
    
    if (hgt > 480 || wid < 320) {
        hgt = 480;
        wid = 640;
        // 图片自适应高度
        CGFloat height = hgt * (SCREEN_WIDTH - 20)/wid;
        CGFloat h1 = 60 + [HTVideoTableViewCell heightOfTitle:configureModel];
        _videoImageView.frame = CGRectMake(10, h1 + 10, SCREEN_WIDTH - 20, height);
        _playButton.frame = CGRectMake(SCREEN_WIDTH/2 - 35, height/2 - 25, 50, 50);
        _playTimeLabel.frame = CGRectMake(0, height - 15, 100, 15);
        _playTimeLabel.text = [NSString stringWithFormat:@"%@播放", configureModel.video.playcount];
        _videoTimeLabel.frame = CGRectMake(SCREEN_WIDTH - 55, height - 15, 40, 15);
        NSInteger min = [configureModel.video.duration floatValue] / 60;
        NSInteger ms = [configureModel.video.duration intValue] % 60;
        _videoTimeLabel.text = [NSString stringWithFormat:@"%ld:%ld", min, ms];
        [_videoImageView sd_setImageWithURL:[NSURL URLWithString:configureModel.video.thumbnail[0]]];
        if (configureModel.top_comment.content == nil) {
            CGFloat h4 = h1 + height + 10;
            NSMutableArray *arr = [NSMutableArray array];
            for (HTTagsModel *model in configureModel.tagsArray) {
                [arr addObject:model.name];
            }
            _tagsImageView.frame = CGRectMake(10, h4 + 15, 20, 20);
            _tagsLabel.text = [arr componentsJoinedByString:@" "];
            _tagsLabel.frame = CGRectMake(40, h4 + 10, SCREEN_WIDTH - 50, 30);
            
            _upBLView.frame = CGRectMake(20, h4 + 50, kWidth, 20);
            _upBLView.label.text = configureModel.up;
            _downBLView.frame = CGRectMake(20 + kWidth + 50, h4 + 50, kWidth, 20);
            _downBLView.label.text = configureModel.down;
            _shareBLView.frame = CGRectMake(20 + kWidth*2 + 100, h4 + 50, kWidth, 20);
            _shareBLView.label.text = configureModel.forward;
            _commentBLView.frame = CGRectMake(20 + 3*kWidth + 150, h4 + 50, kWidth, 20);
            _commentBLView.label.text = configureModel.comment;
            
            _hotComment.frame = CGRectZero;
            _detailComment.frame = CGRectZero;
        }else{
            CGFloat h2 = h1 + height + 20;
            
            _hotComment.frame = CGRectMake(10, h2, 60, 20);
            
            _detailComment.frame = CGRectMake(10, h2 + 25, SCREEN_WIDTH - 20, [HTVideoTableViewCell heightOfDetailComment:configureModel]);
            _detailComment.text = [NSString stringWithFormat:@"%@: %@", configureModel.top_comment.u.name, configureModel.top_comment.content];
            
            NSMutableArray *arr = [NSMutableArray array];
            for (HTTagsModel *model in configureModel.tagsArray) {
                [arr addObject:model.name];
            }
            CGFloat h3 = h2 + 20 + 10 + [HTVideoTableViewCell heightOfDetailComment:configureModel];
            _tagsLabel.text = [arr componentsJoinedByString:@" "];
            _tagsLabel.frame = CGRectMake(40, h3 , SCREEN_WIDTH - 50, 30);
            _tagsImageView.frame = CGRectMake(10, h3 + 5, 20, 20);
            
            _upBLView.frame = CGRectMake(20, h3 + 40, kWidth, 20);
            _upBLView.label.text = configureModel.up;
            _downBLView.frame = CGRectMake(20 + kWidth + 50, h3 + 40, kWidth, 20);
            _downBLView.label.text = configureModel.down;
            _shareBLView.frame = CGRectMake(20 + kWidth*2 + 100, h3 + 40, kWidth, 20);
            _shareBLView.label.text = configureModel.forward;
            _commentBLView.frame = CGRectMake(20 + 3*kWidth + 150, h3 + 40, kWidth, 20);
            _commentBLView.label.text = configureModel.comment;
        }

    }else{
        // 图片自适应高度
        CGFloat height = hgt * (SCREEN_WIDTH - 20)/[configureModel.video.width floatValue];
        CGFloat h1 = 60 + [HTVideoTableViewCell heightOfTitle:configureModel];
        _videoImageView.frame = CGRectMake(10, h1 + 10, SCREEN_WIDTH - 20, height);
        _playButton.frame = CGRectMake(SCREEN_WIDTH/2 - 35, height/2 - 25, 50, 50);
        _playTimeLabel.frame = CGRectMake(0, height - 15, 100, 15);
        _playTimeLabel.text = [NSString stringWithFormat:@"%@播放", configureModel.video.playcount];
        _videoTimeLabel.frame = CGRectMake(SCREEN_WIDTH - 50, height - 15, 40, 15);
        NSInteger min = [configureModel.video.duration floatValue] / 60;
        NSInteger ms = [configureModel.video.duration intValue] % 60;
        _videoTimeLabel.text = [NSString stringWithFormat:@"%ld:%ld", min, ms];
        [_videoImageView sd_setImageWithURL:[NSURL URLWithString:configureModel.video.thumbnail[0]]];
        if (configureModel.top_comment.content == nil) {
            CGFloat h4 = h1 + height + 10;
            NSMutableArray *arr = [NSMutableArray array];
            for (HTTagsModel *model in configureModel.tagsArray) {
                [arr addObject:model.name];
            }
            _tagsImageView.frame = CGRectMake(10, h4 + 15, 20, 20);
            _tagsLabel.text = [arr componentsJoinedByString:@" "];
            _tagsLabel.frame = CGRectMake(40, h4 + 10, SCREEN_WIDTH - 50, 30);
            
            _upBLView.frame = CGRectMake(20, h4 + 50, kWidth, 20);
            _upBLView.label.text = configureModel.up;
            _downBLView.frame = CGRectMake(20 + kWidth + 50, h4 + 50, kWidth, 20);
            _downBLView.label.text = configureModel.down;
            _shareBLView.frame = CGRectMake(20 + kWidth*2 + 100, h4 + 50, kWidth, 20);
            _shareBLView.label.text = configureModel.forward;
            _commentBLView.frame = CGRectMake(20 + 3*kWidth + 150, h4 + 50, kWidth, 20);
            _commentBLView.label.text = configureModel.comment;
            
            _hotComment.frame = CGRectZero;
            _detailComment.frame = CGRectZero;
        }else{
            CGFloat h2 = h1 + height + 20;
            
            _hotComment.frame = CGRectMake(10, h2, 60, 20);
            
            _detailComment.frame = CGRectMake(10, h2 + 25, SCREEN_WIDTH - 20, [HTVideoTableViewCell heightOfDetailComment:configureModel]);
            _detailComment.text = [NSString stringWithFormat:@"%@: %@", configureModel.top_comment.u.name, configureModel.top_comment.content];
            
            NSMutableArray *arr = [NSMutableArray array];
            for (HTTagsModel *model in configureModel.tagsArray) {
                [arr addObject:model.name];
            }
            CGFloat h3 = h2 + 20 + 10 + [HTVideoTableViewCell heightOfDetailComment:configureModel];
            _tagsLabel.text = [arr componentsJoinedByString:@" "];
            _tagsLabel.frame = CGRectMake(40, h3 , SCREEN_WIDTH - 50, 30);
            _tagsImageView.frame = CGRectMake(10, h3 + 5, 20, 20);
            
            _upBLView.frame = CGRectMake(20, h3 + 40, kWidth, 20);
            _upBLView.label.text = configureModel.up;
            _downBLView.frame = CGRectMake(20 + kWidth + 50, h3 + 40, kWidth, 20);
            _downBLView.label.text = configureModel.down;
            _shareBLView.frame = CGRectMake(20 + kWidth*2 + 100, h3 + 40, kWidth, 20);
            _shareBLView.label.text = configureModel.forward;
            _commentBLView.frame = CGRectMake(20 + 3*kWidth + 150, h3 + 40, kWidth, 20);
            _commentBLView.label.text = configureModel.comment;
        }
    }
}
+ (CGFloat)heightForCell:(HTVideoMainListModel *)model
{
    CGFloat titleHeight = [HTVideoTableViewCell heightOfTitle:model];
    CGFloat commentHeight = [HTVideoTableViewCell heightOfDetailComment:model];
    CGFloat h = [model.video.height floatValue];
    CGFloat w = [model.video.width floatValue];
    if (h > 480 || w < 320) {
        CGFloat imageHeight = 480 * (SCREEN_WIDTH - 20) / 480;
        NSString *str = [NSString stringWithFormat:@"%@: %@", model.top_comment.u.name, model.top_comment.content];
        //    NSLog(@"%@", str);
        if ([str isEqualToString:@"(null): (null)"]) {
            return titleHeight + imageHeight + 160;
        }if ([model.video.height floatValue] > 480) {
            return titleHeight + commentHeight + imageHeight + 90;
        }
        return titleHeight + commentHeight + imageHeight + 200;
    }else{
        CGFloat imageHeight = h * (SCREEN_WIDTH - 20)/w;
        NSString *str = [NSString stringWithFormat:@"%@: %@", model.top_comment.u.name, model.top_comment.content];
        //    NSLog(@"%@", str);
        if ([str isEqualToString:@"(null): (null)"]) {
            return titleHeight + imageHeight + 160;
        }
        return titleHeight + commentHeight + imageHeight + 200;
    }
}

+ (CGFloat)heightOfTitle:(HTVideoMainListModel *)model
{
    // 标题文本
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
        CGRect bounds1 = [model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
   
    return bounds1.size.height;
}

+ (CGFloat)heightOfDetailComment:(HTVideoMainListModel *)model
{
    // 热门评论
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    NSString *str = [NSString stringWithFormat:@"%@: %@", model.top_comment.u.name, model.top_comment.content];
     CGRect bounds2 = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds2.size.height;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _illView = [[ILLView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH/2, 40)];
        
        _connectionButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _connectionButton.frame = CGRectMake(SCREEN_WIDTH - 50, 15, 30, 25);
        [_connectionButton setBackgroundImage:[UIImage imageNamed:@"connection"] forState:(UIControlStateNormal)];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, SCREEN_WIDTH - 30, 50)];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.numberOfLines = 0;
        
        _videoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 110, SCREEN_WIDTH - 20, 100)];
        _playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_playButton setBackgroundImage:[UIImage imageNamed:@"bofang"] forState:(UIControlStateNormal)];
        _playTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 85, 100, 15)];
        _videoTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 85, 40, 15)];
        _playTimeLabel.textColor = [UIColor whiteColor];
        _playTimeLabel.font = [UIFont systemFontOfSize:12];
        _videoTimeLabel.textColor = [UIColor whiteColor];
        _videoTimeLabel.font = [UIFont systemFontOfSize:12];
        
        [self.videoImageView addSubview:_playButton];
        [self.videoImageView addSubview:_playTimeLabel];
        [self.videoImageView addSubview:_videoTimeLabel];
        
        _tagsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 305, 20 , 20)];
        _tagsImageView.image = [UIImage imageNamed:@"便签"];
        
        _tagsLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 300, SCREEN_WIDTH - 50, 30)];
        _tagsLabel.font = [UIFont systemFontOfSize:13];
        _tagsLabel.textColor = [UIColor lightGrayColor];
        
        _hotComment = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 60, 20)];
        _hotComment.backgroundColor = [UIColor lightGrayColor];
        _hotComment.textColor = [UIColor whiteColor];
        _hotComment.text = @"最新评论";
        _hotComment.font = [UIFont systemFontOfSize:14];
        _detailComment = [[UILabel alloc]initWithFrame:CGRectMake(10, 250, SCREEN_WIDTH - 20, 100)];
        _detailComment.backgroundColor = [UIColor colorWithWhite:0.960 alpha:1.000];
        _detailComment.font = [UIFont systemFontOfSize:14];
        _detailComment.numberOfLines = 0;

        
        _upBLView = [[BLView alloc]initWithFrame:CGRectMake(20, 350, kWidth, 20)];
        [_upBLView.button setBackgroundImage:[UIImage imageNamed:@"点赞"] forState:(UIControlStateNormal)];
        _upBLView.label.textColor = [UIColor colorWithWhite:0.772 alpha:1.000];
        
        _downBLView = [[BLView alloc]initWithFrame:CGRectMake(20 + kWidth + 50, 350, kWidth, 20)];
        [_downBLView.button setBackgroundImage:[UIImage imageNamed:@"差评"] forState:(UIControlStateNormal)];
        
        _commentBLView = [[BLView alloc]initWithFrame:CGRectMake(20 + kWidth*2 + 100, 350, kWidth, 20)];
        [_commentBLView.button setBackgroundImage:[UIImage imageNamed:@"评论"] forState:(UIControlStateNormal)];
        
        _shareBLView = [[BLView alloc]initWithFrame:CGRectMake(20 + 3*kWidth + 150, 350, kWidth, 20)];
        [_shareBLView.button setBackgroundImage:[UIImage imageNamed:@"分享"] forState:(UIControlStateNormal)];
        
        _videoImageView.backgroundColor = [UIColor colorWithRed:1.000 green:0.639 blue:0.574 alpha:1.000];
        
        [self.contentView addSubview:_illView];
        [self.contentView addSubview:_connectionButton];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_videoImageView];
        [self.contentView addSubview:_tagsImageView];
        [self.contentView addSubview:_tagsLabel];
        [self.contentView addSubview:_upBLView];
        [self.contentView addSubview:_downBLView];
        [self.contentView addSubview:_shareBLView];
        [self.contentView addSubview:_commentBLView];
        [self.contentView addSubview:_hotComment];
        [self.contentView addSubview:_detailComment];

    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
